package com.ruoyi.web.controller.system;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ResourceValues;
import com.ruoyi.framework.config.ServerConfig;
import org.apache.catalina.webresources.FileResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/file")
public class FileUploadController {
    @Autowired
    private ServerConfig serverConfig;
    /**
     * @param //files 批量上传的文件
     * @return 上传成功条数
     */
    @PostMapping("/uploadfilePIC")
    public AjaxResult uploadByPIC(@RequestParam("file") MultipartFile[] file,  Integer filePostion) {
        int count1 = -1;
        String url="";
        try {
            for (MultipartFile files : file) {
                long size = files.getSize();
                if (!FileUploadUtils.getExtension(files).equals("png") && !FileUploadUtils.getExtension(files).equals("jpg")
                        && !FileUploadUtils.getExtension(files).equals("JPG") &&
                        !FileUploadUtils.getExtension(files).equals("PNG") && !FileUploadUtils.getExtension(files).equals("jpeg")
                        && !FileUploadUtils.getExtension(files).equals("JPEG")
                        && !FileUploadUtils.getExtension(files).equals("gif")
                        && !FileUploadUtils.getExtension(files).equals("GIF")) {
                    return AjaxResult.error("文件格式错误请上传png/jpg/jpeg/gif格式文件！");
                }
                int fileNamelength = files.getOriginalFilename().length();
                if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
                    return AjaxResult.error("文件名字过长");
                }
                if ((10 * 1024 * 1024) != -1 &&
                        size > (10 * 1024 * 1024)) {
                    return AjaxResult.error("文件过大，请重新上传");
                }
                //重新写一个编码格式，将工单号拼到图片名中
                String fileName = FileUploadUtils.extractFilename(files);
                System.out.println("abcdefg" + fileName);
                url=url+serverConfig.getUrl() + Constants.RESOURCE_PREFIX + "/" +fileName+",";
                File desc = new File(FileUploadUtils.getDefaultBaseDir() + File.separator + fileName);
                if (!desc.getParentFile().exists()) {
                    desc.getParentFile().mkdirs();
                }
                if (!desc.exists()) {
                    desc.createNewFile();
                }
                files.transferTo(desc);
            }
            System.out.println("77777777777777777777777777"+url.substring(0,url.length()-1));

            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url.substring(0,url.length()-1));
            return ajax;
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("上传错误，请重试！");
        }
    }


}
